package com.example.wms;

import android.content.Intent;
import android.os.Bundle;
import android.app.Activity;
import android.widget.Button;
import android.widget.Toast;
import com.example.classes.*;

public class LoginHandler extends Activity{

    Button loginB;
    String sampleU = "joe";
    String sampleP ="bob";
    String Username= new String("");
    String Password= new String("");
    Database db;
    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        GlobalApplication ga = (GlobalApplication)getApplicationContext();
        db = ga.getDb();
        Database db= new Database(this);
        setContentView(R.layout.main);
        Intent intent=getIntent();
        Username=intent.getStringExtra(StartupActivity.USER_ID);
        Password=intent.getStringExtra(StartupActivity.PASS);
        if(loginCheck(Username,Password))
        {
            db.close();
            Toast.makeText(LoginHandler.this, "Success",Toast.LENGTH_LONG).show();
            Intent homeIntent = new Intent(this, HomeHandler.class);
            homeIntent.putExtra("username",Username);
            homeIntent.putExtra("email",getEmail(Username));
            startActivity(homeIntent);
        }
        else Toast.makeText(LoginHandler.this, "u r suck h4k3r n00b",Toast.LENGTH_LONG).show();

        finish();


    }

    public boolean loginCheck(String uname, String upass){
        Member mb = db.getEntry(uname);
        if (mb != null && mb.getPassword().equals(Encryptor.encrypt(upass)))
            return true;
        return false;
    }

    public String getEmail(String uname){
        Member mb = db.getEntry(uname);
        if (mb != null)
            return mb.getContact();
        return "No email listed.";
    }

}